<?php


namespace app\base\service;

use think\admin\service\AdminService;

use think\Cache;

/**
 * 阿里百炼服务
 * Class AdminService
 * @package think\admin\service
 */
class TikhubService extends AdminService
{

    protected $authorization = 'opsBHPwrh32uqX+1Nsex8ATWMmehbMFushro62ujdPa4zmTtJHKgcmK/FQ==';

    protected $url = 'https://api.tikhub.io';
    //protected $url = 'https://beta.tikhub.io';
    //超频调用返回code值
    protected $code = ["Throttling","Throttling.RateQuota","Throttling.AllocationQuota","insufficient_quota","LimitRequests","limit_requests"];

    protected function resulet($code=0,$msg='',$data=[])
    {
        return [
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ];
    }

    //获取小红书用户笔记
    public function getXhsUserNotes($user_id)
    {

       // $url = $this->url."/api/v1/xiaohongshu/web_v2/fetch_home_notes?user_id={$user_id}";
        $url = $this->url."/api/v1/xiaohongshu/web/get_user_info_v3?user_id={$user_id}";
        $data = [
          "user_id"=>$user_id,
          "lastCursor"=>""
        ];
        $options['headers'] = ['Authorization:Bearer '.$this->authorization];
        $options['timeout'] = 600;
        $retryCount = 0;
        do{
            $res = http_get($url,[],$options);
            $res = json_decode($res,true);
            dump($res);exit;
            if(empty($res)){
                return  $this->resulet(0,'获取小红书用户笔记失败');
            }
            if(isset($res['data']) && $res['code']==200){
                return  $this->resulet(1,'',$res['data']);

            }else{
                $this->app->log->error('获取小红书用户笔记错误:'.json_encode($res));
                if(isset($res['detail']) ){
                    if(is_array($res['detail']) && ($res['detail']['code']==429 || $res['detail']['code']==500)|| $res['detail']['code']==400){
                        sleep(10);
                        $retryCount++;
                    }else{
                        return  $this->resulet(0,'获取小红书用户笔记失败:'.$res['detail']);
                    }
                }else{
                    return  $this->resulet(0,$res);
                }
            }
        }while($retryCount < 10);

    }

    //获取小红书笔记详情(分享链接)
    public function getXhsNoteInfo($note_id="",$share_text="")
    {
        $url = $this->url."/api/v1/xiaohongshu/web/get_note_info_v4";
        $data = [
            "note_id"=>$note_id,
            "share_text"=>$share_text,
        ];
        $options['headers'] = ['Authorization:Bearer '.$this->authorization];
        $options['timeout'] = 600;
        $retryCount = 0;
        do{
            $res = http_get($url,$data,$options);
            $res = json_decode($res,true);
            dump($res);
            if(empty($res)){
                return  $this->resulet(0,'获取小红书用户笔记失败');
            }
            if(isset($res['data']) && $res['code']==200){
                return  $this->resulet(1,'',$res['data']);

            }else{
                $this->app->log->error('获取小红书用户笔记错误:'.json_encode($res));
                if(isset($res['detail']) ){
                    if(is_array($res['detail']) && ($res['detail']['code']==429 || $res['detail']['code']==500)){
                        sleep(60);
                        $retryCount++;
                    }else{
                        return  $this->resulet(0,'获取小红书用户笔记失败:'.$res['detail']);
                    }
                }else{
                    return  $this->resulet(0,$res);
                }
            }
        }while($retryCount < 10);
    }


    //获取小红书笔记详情(笔记ID)
    public function getXhsNoteInfoById($note_id)
    {
        $url = $this->url."/api/v1/xiaohongshu/web_v2/fetch_feed_notes";
        $data = [
            "note_id"=>$note_id,
        ];
        $options['headers'] = ['Authorization:Bearer '.$this->authorization];
        $options['timeout'] = 600;
        $retryCount = 0;
        do{
            $res = http_get($url,$data,$options);
            $res = json_decode($res,true);

            if(empty($res)){
                return  $this->resulet(0,'获取小红书用户笔记失败');
            }
            if(isset($res['data']) && $res['code']==200){
                return  $this->resulet(1,'',$res['data']);

            }else{
                $this->app->log->error('获取小红书用户笔记错误:'.json_encode($res));
                if(isset($res['detail']) ){
                    if(is_array($res['detail']) && ($res['detail']['code']==429 || $res['detail']['code']==500)){
                        sleep(60);
                        $retryCount++;
                    }else{
                        return  $this->resulet(0,'获取小红书用户笔记失败:'.$res['detail']);
                    }
                }else{
                    return  $this->resulet(0,$res);
                }
            }
        }while($retryCount < 10);
    }

    //获取抖音用户视频
    public function getDyUserVideos($user_id){
        $url = $this->url."/api/v1/douyin/app/v3/fetch_user_post_videos";
        $data = [
            "sec_user_id"=>$user_id,
            "max_cursor"=>0,
            "counts"=>20
        ];
        $options['headers'] = ['Authorization:Bearer '.$this->authorization];
        $options['timeout'] = 600;
        $retryCount = 0;
        do{
            $res = http_get($url,$data,$options);
            $res = json_decode($res,true);
           // dump($res);
            if(empty($res)){
                if($retryCount>9){
                    return  $this->resulet(0,'获取抖音账号数据失败');
                }else{
                    sleep(60);
                    $retryCount++;
                }
            }
            if(isset($res['data']) && $res['code']==200){
                return  $this->resulet(1,'',$res['data']);

            }else{
                $this->app->log->error('获取抖音账号数据错误:'.json_encode($res));
                if(isset($res['detail']) ){
                    sleep(60);
                    $retryCount++;
//                    if(is_array($res['detail']) && ($res['detail']['code']==429 || $res['detail']['code']==500 || $res['detail']['code']==404 || $res['detail']['code']==401)){
//                        sleep(60);
//                        $retryCount++;
//                    }else{
//                        if(is_array($res['detail'])){
//                            return  $this->resulet(0,'获取抖音账号数据失败:'.$res['detail']['message']);
//                        }else{
//                            return  $this->resulet(0,'获取抖音账号数据失败:'.$res['detail']);
//                        }
//
//
//                    }
                }else{
                    return  $this->resulet(0,$res);
                }
            }
        }while($retryCount < 10);
        return  $this->resulet(0,'获取抖音账号数据失败');
    }

}
